const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const mongoose = require('mongoose')
const {connect , initSchemas } = require('./database/init.js')
const bodyParser = require('koa-bodyparser')
const cors = require('koa2-cors')

app.use(bodyParser())
app.use(cors())

let user = require('./appApi/user.js')
let goods = require('./appApi/goods.js')
// let category = require('./appApi/category.js')

let router = new Router()
router.use('/user', user.routes())
router.use('/goods', goods.routes())
// router.use('/category', category.routes())


;(async () => {
  await connect()
  initSchemas()
  // const User = mongoose.model('User')
  // let oneUser = new User({username:'zhansan3', password: '123456'})
  // oneUser.save().then( () => {
  //   console.log('数据插入成功')
  // })
  // let user = await User.findOne({}).exec()
  // console.log('----------------------------')
  // console.log(user)
  // console.log('----------------------')
})()

app.use(router.routes())
app.use(router.allowedMethods())
app.use(async ctx => {
  ctx.body = 'hello word'
})

app.listen(3000, () => {
  console.log('服务器启动---3000')
})